package com.gzhryc.shared_device.oem.h5.mng.controllers.bxscn;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.bxscn.code.dao.db.PortableEnergyCabinet;
import com.gzhryc.shared_device.bxscn.code.dao.enums.PortableEnergyCabinetEnum;
import com.gzhryc.shared_device.bxscn.code.services.PortableEnergyCabinetService;
import com.gzhryc.shared_device.bxscn.core.IBXSCNNetworkService;
import com.gzhryc.shared_device.bxscn.core.models.BXSCNExecuteResult;
import com.gzhryc.shared_device.bxscn.core.models.BXSCNLeaseCommand;
import com.gzhryc.shared_device.bxscn.core.models.BXSCNOpenSlotCommand;
import com.gzhryc.shared_device.oem.code.HttpNetworkServiceUtils;
import com.gzhryc.shared_device.oem.code.device.BXSCNCacheManager;

import javax.servlet.http.HttpServletRequest;

public class PortableEnergyCabinetOperateController {

    @Route(value = {"/bxscn/device/operate/search.do"})
    public AjaxResult doSearch(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "便携式储能电池查询失败");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        if (StringTools.isNotBlank(deviceSn)) {
            PortableEnergyCabinet portableEnergyCabinet = PortableEnergyCabinetService.self().getByDeviceSn(deviceSn);
            if (portableEnergyCabinet != null) {
                if(PortableEnergyCabinetEnum.State.Online.index().equals(portableEnergyCabinet.getState())) {
                    //如果缓存不存在，代表没有上线
                    PortableEnergyCabinet temp = BXSCNCacheManager.self().getPortableEnergyCabinet(portableEnergyCabinet.getDeviceSn());
                    if(temp == null){
                        PortableEnergyCabinetService.self().offline(portableEnergyCabinet.getDeviceSn());
                    }
                }

                IBXSCNNetworkService networkService = HttpNetworkServiceUtils.getBXSCNNetworkService(portableEnergyCabinet.getIotNodeId());
                if (networkService != null) {
                    BXSCNExecuteResult executeResult = networkService.searchDevice(deviceSn);
                    if (executeResult != null) {
                        if (BXSCNExecuteResult.SUCCESS.equals(executeResult.getCode())) {
                            result.setCode(DtoConstants.SUCCESS, null);
                        } else if (StringTools.isNotBlank(executeResult.getMsg())) {
                            result.setMsg(executeResult.getMsg());
                        } else {
                            result.setMsg("发送命令失败");
                        }
                    } else {
                        result.setMsg("便携式储能电池机柜网络服务无响应");
                    }
                } else {
                    result.setMsg("未获取便携式储能电池机柜网络服务");
                }
            }
        }
        return result;
    }

    @Route("/bxscn/device/operate/lease.html")
    public String lease(HttpServletRequest request) {
        return "/bxscn/device/lease.html";
    }

    @Route(value = {"/bxscn/device/operate/lease.do"})
    public AjaxResult doLease(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "自动租借失败");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        String tradeNo = WebUtils.getString(request, "tradeNo");
        Integer slotNum = WebUtils.getInteger(request, "slotNum");
        if (StringTools.isNotBlank(deviceSn) && StringTools.isNotBlank(tradeNo)) {
            PortableEnergyCabinet portableEnergyCabinet = PortableEnergyCabinetService.self().getByDeviceSn(deviceSn);
            if (portableEnergyCabinet != null) {
                IBXSCNNetworkService networkService = HttpNetworkServiceUtils.getBXSCNNetworkService(portableEnergyCabinet.getIotNodeId());
                if (networkService != null) {
                    BXSCNLeaseCommand command = new BXSCNLeaseCommand();
                    command.setDeviceSn(deviceSn);
                    command.setTradeNo(tradeNo);
                    command.setSlotNum(slotNum);

                    BXSCNExecuteResult executeResult = networkService.lease(command);
                    if (executeResult != null) {
                        if (BXSCNExecuteResult.SUCCESS.equals(executeResult.getCode())) {
                            result.setCode(DtoConstants.SUCCESS, null);
                        } else if (StringTools.isNotBlank(executeResult.getMsg())) {
                            result.setMsg(executeResult.getMsg());
                        } else {
                            result.setMsg("发送命令失败");
                        }
                    } else {
                        result.setMsg("便携式储能电池机柜网络服务无响应");
                    }
                } else {
                    result.setMsg("未获取便携式储能电池机柜网络服务");
                }
            }
        }
        return result;
    }

    @Route(value = {"/bxscn/device/operate/open_slot.do"})
    public AjaxResult doOpenSlot(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "自动租借失败");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        Integer slotNum = WebUtils.getInteger(request, "slotNum");
        if (StringTools.isNotBlank(deviceSn)) {
            PortableEnergyCabinet portableEnergyCabinet = PortableEnergyCabinetService.self().getByDeviceSn(deviceSn);
            if (portableEnergyCabinet != null) {
                IBXSCNNetworkService networkService = HttpNetworkServiceUtils.getBXSCNNetworkService(portableEnergyCabinet.getIotNodeId());
                if (networkService != null) {
                    BXSCNOpenSlotCommand command = new BXSCNOpenSlotCommand();
                    command.setDeviceSn(deviceSn);
                    command.setSlotNum(slotNum);

                    BXSCNExecuteResult executeResult = networkService.openSlot(command);
                    if (executeResult != null) {
                        if (BXSCNExecuteResult.SUCCESS.equals(executeResult.getCode())) {
                            result.setCode(DtoConstants.SUCCESS, null);
                        } else if (StringTools.isNotBlank(executeResult.getMsg())) {
                            result.setMsg(executeResult.getMsg());
                        } else {
                            result.setMsg("发送命令失败");
                        }
                    } else {
                        result.setMsg("便携式储能电池机柜网络服务无响应");
                    }
                } else {
                    result.setMsg("未获取便携式储能电池机柜网络服务");
                }
            }
        }
        return result;
    }

    @Route(value = {"/bxscn/device/operate/eject_all.do"})
    public AjaxResult doEjectAll(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "打开全部仓门失败");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        if (StringTools.isNotBlank(deviceSn)) {
            PortableEnergyCabinet portableEnergyCabinet = PortableEnergyCabinetService.self().getByDeviceSn(deviceSn);
            if (portableEnergyCabinet != null) {
                IBXSCNNetworkService networkService = HttpNetworkServiceUtils.getBXSCNNetworkService(portableEnergyCabinet.getIotNodeId());
                if (networkService != null) {
                    BXSCNExecuteResult executeResult = networkService.forceEjectDevice(portableEnergyCabinet.getDeviceSn());
                    if (executeResult != null) {
                        if (BXSCNExecuteResult.SUCCESS.equals(executeResult.getCode())) {
                            result.setCode(DtoConstants.SUCCESS, null);
                        } else if (StringTools.isNotBlank(executeResult.getMsg())) {
                            result.setMsg(executeResult.getMsg());
                        } else {
                            result.setMsg("发送命令失败");
                        }
                    } else {
                        result.setMsg("便携式储能电池机柜网络服务无响应");
                    }
                } else {
                    result.setMsg("未获取便携式储能电池机柜网络服务");
                }
            }
        }
        return result;
    }

    @Route(value = {"/bxscn/device/operate/eject.do"})
    public AjaxResult doEject(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "开仓失败");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        Integer slotNum = WebUtils.getInteger(request, "slotNum");
        if (StringTools.isNotBlank(deviceSn) && slotNum != null) {
            PortableEnergyCabinet portableEnergyCabinet = PortableEnergyCabinetService.self().getByDeviceSn(deviceSn);
            if (portableEnergyCabinet != null) {
                IBXSCNNetworkService networkService = HttpNetworkServiceUtils.getBXSCNNetworkService(portableEnergyCabinet.getIotNodeId());
                if (networkService != null) {
                    BXSCNExecuteResult executeResult = networkService.forceEjectDevice(deviceSn,slotNum);
                    if (executeResult != null) {
                        if (BXSCNExecuteResult.SUCCESS.equals(executeResult.getCode())) {
                            result.setCode(DtoConstants.SUCCESS, null);
                        } else if (StringTools.isNotBlank(executeResult.getMsg())) {
                            result.setMsg(executeResult.getMsg());
                        } else {
                            result.setMsg("发送命令失败");
                        }
                    } else {
                        result.setMsg("便携式储能电池机柜网络服务无响应");
                    }
                } else {
                    result.setMsg("未获取便携式储能电池机柜网络服务");
                }
            }
        }
        return result;
    }

}
